package com.util;

import android.annotation.SuppressLint;
import android.app.Activity;

import androidx.annotation.Nullable;

import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

public class DialogUtil {
    @SuppressLint("NewApi")
    public static QMUITipDialog CreateDoalog(Activity activity,String data,int sleep,@QMUITipDialog.Builder.IconType int iconType){

        final QMUITipDialog[] qemu = new QMUITipDialog[1];
        activity.runOnUiThread(() -> qemu[0] = new QMUITipDialog.Builder(activity)
                .setIconType(iconType)
                .setTipWord(data)
                .create());
        sleep=sleep==0?2000:sleep;
        activity.runOnUiThread(() -> {
            qemu[0].create();
            qemu[0].show();
        });
        int finalSleep = sleep;
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(finalSleep);
                    activity.runOnUiThread(qemu[0]::cancel);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        return qemu[0];
    }
    public static QMUITipDialog CreateLog(Activity activity, String data,int sleep){
        QMUITipDialog qemu= new QMUITipDialog.Builder(activity)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord(data)
                .create();
        activity.runOnUiThread(qemu::show);
        if(sleep==0)return qemu;
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(sleep);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                activity.runOnUiThread(qemu::dismiss);
            }
        }.start();
        return qemu;
    }
}
